package com.ztjy.purchase.controller;

import com.github.pagehelper.PageInfo;
import com.ztjy.annonation.MyLog;
import com.ztjy.bean.CProduct;
import com.ztjy.bean.CType;
import com.ztjy.purchase.biz.CProductBiz;
import com.ztjy.purchase.biz.CTypeBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;
//商品管理
@Controller
@RequestMapping("/purchase/product")

public class CProductController {
    @Autowired
    private CProductBiz pbiz;
    @Autowired
    private CTypeBiz tbiz;

    @ModelAttribute
    public void initModuleData(HttpSession session) {
        session.removeAttribute("mid");
        session.setAttribute("mid", 19);
    }
    @MyLog(value = "查询所有商品管理操作")
    @RequestMapping("/cplist")
    public String All(CProduct product, @RequestParam(value = "pageno",defaultValue = "1")Integer pageNo, Model model){
        Integer pageSize=6;
        PageInfo<CProduct> pageProduct = pbiz.findPageProduct(product, pageNo, pageSize);
        model.addAttribute("Cpage",pageProduct);
        model.addAttribute("pro",product);
        return "forward:/purchase/product/productList.html";
    }
    @MyLog(value = "添加商品管理操作")
    @RequestMapping("/addPro")
    public String add(CProduct product){
        boolean flag = pbiz.addProduct(product);
        if(flag){
            return "redirect:/purchase/product/cplist";
        }else{
            return "forward:/purchase/product/productAdd.html";
        }
    }
    @MyLog(value = "跳入修改")
    @RequestMapping("/goupdate/{id}")
    public String goupdate(@PathVariable("id")Integer productId, Model model){
        CProduct product = pbiz.findByIdProduct(productId);
        model.addAttribute("Pedit",product);
        List<CType> allCType = tbiz.findAllCType();
        model.addAttribute("typesa",allCType);
        return "forward:/purchase/product/productUpdate";
    }

    @MyLog(value = "修改方法")
    @RequestMapping("doupdate")
    public String doupdate(CProduct product){
        boolean flag = pbiz.updateProduct(product);
        if(flag){
            return "redirect:/purchase/product/cplist";
        }else{
            return "forward:/purchase/product/productUpdate.html";
        }
    }

    @MyLog(value = "状态修改")
    @RequestMapping("/updateZt")
    public String zx(Integer productId ,Model model,Integer productStatus ){
        CProduct product = pbiz.findByIdProduct(productId);
        if(product.getProductStatus()==1){
            product.setProductStatus(2);
        }else if(product.getProductStatus()==2&&product.getTypes().getTypeStatus()==1){
            product.setProductStatus(1);
        }
        pbiz.updateProduct(product);
        return "redirect:/purchase/product/cplist";
    }

}
